package com.bootdo.blog.dao;

import com.bootdo.blog.domain.TargetDO;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-03-12 07:33:16
 */
@Mapper
public interface TargetMapper {

	@Select("select `target_id`, `project`, `indicator` from tb_target where target_id = #{id}")
	TargetDO get(Integer targetId);
	
	@Select("<script>" +
	"select * from tb_target " + 
			"<where>" + 
		  		  "<if test=\"targetId != null and targetId != ''\">"+ "and target_id = #{targetId} " + "</if>" + 
		  		  "<if test=\"project != null and project != ''\">"+ "and project = #{project} " + "</if>" + 
		  		  "<if test=\"indicator != null and indicator != ''\">"+ "and indicator = #{indicator} " + "</if>" + 
		  			"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by target_id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"<if test=\"offset != null and limit != null\">"+
			"limit #{offset}, #{limit}" + 
			"</if>"+
			"</script>")
	List<TargetDO> list(Map<String, Object> map);
	
	@Select("<script>" +
	"select count(*) from tb_target " + 
			"<where>" + 
		  		  "<if test=\"targetId != null and targetId != ''\">"+ "and target_id = #{targetId} " + "</if>" + 
		  		  "<if test=\"project != null and project != ''\">"+ "and project = #{project} " + "</if>" + 
		  		  "<if test=\"indicator != null and indicator != ''\">"+ "and indicator = #{indicator} " + "</if>" + 
		  			"</where>"+ 
			"</script>")
	int count(Map<String, Object> map);
	
	@Insert("insert into tb_target (`project`, `indicator`)"
	+ "values (#{project}, #{indicator})")
	int save(TargetDO target);
	
	@Update("<script>"+ 
			"update tb_target " + 
					"<set>" + 
		            "<if test=\"targetId != null\">`target_id` = #{targetId}, </if>" + 
                    "<if test=\"project != null\">`project` = #{project}, </if>" + 
                    "<if test=\"indicator != null\">`indicator` = #{indicator}, </if>" + 
          					"</set>" + 
					"where target_id = #{targetId}"+
			"</script>")
	int update(TargetDO target);
	
	@Delete("delete from tb_target where target_id =#{targetId}")
	int remove(Integer target_id);
	
	@Delete("<script>"+ 
			"delete from tb_target where target_id in " + 
					"<foreach item=\"targetId\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{targetId}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] targetIds);
}
